﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BookExchange.Business;
using BookExchange.ODataService;
using BookExchange.Utility;
using BookExchange.ViewModel;
using BookExchange.Configuration;

namespace BookExchange.CustomClass.Base
{
    public class BasePage : System.Web.UI.Page
    {
      

        public BasePage()
            : base()
        {
            
        }

        public MemberViewModel CurrentMember
        {
            get
            {
                var member  =Session[AppConstant.CurrentUser] as MemberViewModel;
                if (member == null)
                {
                    member = new MemberViewModel(null);
                }

                return member;
            }
        }

        public bool IsUserAuthenticated
        {
            get
            {
                var member = CurrentMember;

                return (member.MemberCode > 0 && !string.IsNullOrEmpty(member.Email) & !string.IsNullOrEmpty(member.DisplayName));
            }
        }

        public void RedirectToHomepage()
        {
            Response.Redirect("default.aspx");
        }

        public void RedirectToLoginPage(string function, string returnPageIfSuccessful)
        {
            //Response.Redirect("default.aspx");
            Response.Redirect("login.aspx?returnurl=" + returnPageIfSuccessful);            
        }

        public void RedirecToDetailPage(int requestCode)
        {
            Response.Redirect(string.Format("detail.aspx?c={0}", requestCode));
        }

        public void RedirectToMemberDetailPage()
        {
            Response.Redirect(string.Format("MemberDetail.aspx"));
        }

        public void RedirectToErrorPage()
        {
#if DEBUG
            Response.Redirect(string.Format("Error/error.aspx"));
#endif
        }

        protected string GetDate(DateTime? date)
        {
            return date == null ? string.Empty : GetDate(date.Value);
        }

        private string GetDate(DateTime date)
        {
            return date.ToString(AppConfiguration.Instance.DateFormat);
        }

        public string GetDateTime(DateTime? date)
        {
            return date == null ? string.Empty : GetDateTime(date.Value);
        }

        private string GetDateTime(DateTime date)
        {
            return date.ToString(AppConfiguration.Instance.DateTimeFormat);
        }

        //public bool Login(string email, string password, bool rememberMe)
        //{
        //    //Get user in database
            

        //    //VALIDATE

        //    return false;
            
        //}

        public void Login(Member member)
        {
            MemberViewModel memberViewModel = new MemberViewModel(member);
            Session[AppConstant.CurrentUser] = memberViewModel;
        }

        public void Logout()
        {
            Session[AppConstant.CurrentUser] = null;
        }
    }
}